package dth.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.BookMovie;
import dth.entity.Movie;

public class MovieDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	
	public java.util.List<Movie> getListMovie(){
		session.beginTransaction();
		String queryString="select * from movie";
		Query query=session.createSQLQuery(queryString).addEntity(Movie.class);
		return query.list();
	}
	public int delete(int id)
	{
		Transaction tx=session.beginTransaction();
		String queryString="delete from movie where movie_id= :id";
		Query query=session.createSQLQuery(queryString).setParameter("id", id);
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int insert(Movie movie)
	{
		Transaction tx=session.beginTransaction();
		String queryString="INSERT into movie VALUES(0,:name,:price,:fk_admin_id,:date,:image,:des)";
		System.out.println("-----INSERT into movie VALUES(0"+movie.getMovieName()+","+movie.getMoviePrice()+","+","+movie.getFkAdminId());
		Query query=session.createSQLQuery(queryString);
		query.setParameter("name",movie.getMovieName());
		query.setParameter("price",movie.getMoviePrice());
		//query.setParameter("time",movie.getMovieTime());
		query.setParameter("fk_admin_id",movie.getFkAdminId());
		query.setParameter("date",movie.getDateInsert());
		query.setParameter("image",movie.getImage());
		query.setParameter("des",movie.getDescription());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int update(Movie movie)
	{
		Transaction tx=session.beginTransaction();
		String queryString="update movie set movie_description=:des,movie_name=:name,movie_price=:price,fk_admin_id=:fk_ad_id,date_insert=:date,movie_image=:image where movie_id=:id";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("name",movie.getMovieName());
		query.setParameter("price",movie.getMoviePrice());
		query.setParameter("des",movie.getDescription());
		query.setParameter("id",movie.getMovieId());
		query.setParameter("fk_ad_id",movie.getFkAdminId());
		query.setParameter("date",movie.getDateInsert());
		query.setParameter("image",movie.getImage());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	
	public double getPrice(int id) // lay gia cua film
	{
		System.out.println("id film "+ id);
		String queryString="select movie_price from movie where movie_id=:id";
		Object c=((Double)session.createSQLQuery(queryString).setParameter("id",id).uniqueResult()).doubleValue();
		return Double.parseDouble(c.toString());
	}
}
